package com.vanzo.instruction;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.vanzo.Tpplication;
import com.vanzo.encryption.ByteUtil;
import com.vanzo.encryption.Encryption;
import com.vanzo.encryption.R;
import com.vanzo.nfc.NFCManager;
import com.vanzo.ui.BaseActivity;
import com.vanzo.util.LogUtil;

/**
 * Created by tisen on 2018/3/3.
 */

public class TestActivity extends BaseActivity implements View.OnClickListener {
    private Instruction instruction;

    private ScrollView scr;
    private TextView console;
    private Button power;
    private boolean powerState;

    NFCManager nfcMger;

    private int micVolume = 10;
    private int spkVolume = 10;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_instruction_test);
        instruction = ((Tpplication) getApplication()).getInstruction();
        scr = (ScrollView) findViewById(R.id.talkie_con_scr);
        console = (TextView) findViewById(R.id.talkie_console);
        power = (Button) findViewById(R.id.ins_test_power);
        power.setVisibility(View.INVISIBLE);
        findViewById(R.id.ins_test_con).setOnClickListener(this);
        findViewById(R.id.ins_test_discon).setOnClickListener(this);
        findViewById(R.id.ins_test_switch).setOnClickListener(this);
        findViewById(R.id.ins_test_ptt_on).setOnClickListener(this);
        findViewById(R.id.ins_test_ptt_off).setOnClickListener(this);
        findViewById(R.id.ins_test_pw).setOnClickListener(this);
        findViewById(R.id.ins_test_freq).setOnClickListener(this);
        findViewById(R.id.ins_test_mic_add).setOnClickListener(this);
        findViewById(R.id.ins_test_mic_sub).setOnClickListener(this);
        findViewById(R.id.ins_test_vol_add).setOnClickListener(this);
        findViewById(R.id.ins_test_vol_sub).setOnClickListener(this);
        findViewById(R.id.ins_test_version).setOnClickListener(this);
//        findViewById(R.id.ins_test_binary).setOnClickListener(this);

        nfcMger = new NFCManager(this);

        power.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtil.put(this, "power", LogUtil.WARN);
                if (!powerState) {
                    instruction.connect();
                } else {
                    instruction.disconnect();
                }
                powerState = !powerState;
                power.setText("电位:" + powerState);
            }
        });
        instruction.setConsole(new Instruction.Console() {
            @Override
            public void onConsole(String result) {
                Message message = new Message();
                message.obj = result;
                consoleHandler.sendMessage(message);
            }
        });
    }

    private Handler consoleHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            String con = (String) msg.obj;
            if (con.startsWith("AT+")) {
                console.append("\r\n");
                console.append("\r\n send | " + con);
            } else {
                if (con.contains(AT.POWER_STATUS_1)) {
                    Toast.makeText(TestActivity.this, "上电成功", Toast.LENGTH_SHORT).show();
                } else {
                    console.append("\r\n rece | " + con);
                }
            }
            scr.fullScroll(ScrollView.FOCUS_DOWN);
        }
    };


    @Override
    protected void onStart() {
        super.onStart();
//        instruction.start();
        powerState = instruction.isPower();
        power.setText("电位:" + powerState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        try {
            nfcMger.verifyNFC();
        } catch (NFCManager.NFCNotSupported nfcnsup) {
            Toast.makeText(this, "nfc not supportecd", Toast.LENGTH_SHORT).show();
        } catch (NFCManager.NFCNotEnabled nfcnEn) {
            Toast.makeText(this, "nfc not enable", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onDestroy() {
//        instruction.disconnect();
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ins_test_con:
                instruction.sendInstruction(AT.DMO_CONNECT);
                break;
            case R.id.ins_test_discon:
                instruction.sendInstruction(AT.DMO_DISCONNECT);
                break;
            case R.id.ins_test_switch:
                instruction.sendInstruction(AT.DMO_CH_SWITCH + "=0");
                break;
            case R.id.ins_test_ptt_on:
                instruction.sendInstruction(AT.DMO_PTT + "=1");
                break;
            case R.id.ins_test_ptt_off:
                instruction.sendInstruction(AT.DMO_PTT + "=0");
                break;
            case R.id.ins_test_pw:
                instruction.sendInstruction(AT.DMO_SET_PWRSAVELV + "=4");
                break;
            case R.id.ins_test_freq:
                instruction.sendInstruction(AT.DMO_SET_CHFREQ + "=409.7500,409.7500");
                break;
            case R.id.ins_test_mic_add:
                if (micVolume < 31) {
                    micVolume++;
                }
                instruction.sendInstruction(AT.DMO_SETMIC + "=" + micVolume);
                break;
            case R.id.ins_test_mic_sub:
                if (micVolume > 0) {
                    micVolume++;
                }
                instruction.sendInstruction(AT.DMO_SETMIC + "=" + micVolume);
                break;
            case R.id.ins_test_vol_add:
                if (spkVolume < 31) {
                    spkVolume++;
                }
                instruction.sendInstruction(AT.DMO_SET_VOLUME + "=" + spkVolume);
                break;
            case R.id.ins_test_vol_sub:
                if (spkVolume > 0) {
                    spkVolume--;
                }
                instruction.sendInstruction(AT.DMO_SET_VOLUME + "=" + spkVolume);
                break;
            case R.id.ins_test_version:
                instruction.sendInstruction(AT.DMO_VERSION);
                break;
//            case R.id.ins_test_binary:
//                byte[] deviceStatus = Encryption.getDeviceStatus();
//                LogUtil.put(this, "deviceStatus hex string = " + ByteUtil.byteArrayToHexStr(deviceStatus), LogUtil.WARN);
//                instruction.sendInstruction(AT.DMO_BINARY + "=" + ByteUtil.byteArrayToBit(deviceStatus));
//                break;
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Encryption.NFC binary = nfcMger.readBinaryFromTag(intent);
        if (binary != null) {
            console.append("\r\n binary = " + binary.toString());
            instruction.sendInstruction(AT.DMO_BINARY + "=" + ByteUtil.byteArrayToBit(binary.getBinary(), binary.getLength()));
        }
    }
}
